package com.css.dzj.dzjap.modules.export.controller;


import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.enterprise.entity.ApEnterpriseCreditEntity;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseJSService;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseService;
import com.css.dzj.dzjap.modules.expertcredit.entity.ApExpertCreditEntity;
import com.css.dzj.dzjap.modules.expertcredit.service.ExpertCreditService;
import com.css.dzj.dzjap.modules.export.entity.ExcelData;
import com.css.dzj.dzjap.modules.export.service.ExportExcelUtils;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/export")
public class ExportController {

    /**
     * 评价单位信用信息表
     */
    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 建设单位信用信息表
     */
    @Autowired
    private EnterpriseJSService enterpriseJSService;

    /**
     * 专家信用信息表
     */
    @Autowired
    private ExpertCreditService expertCreditService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    private HttpServletRequest request;
    /**
     * 导出评价单位信用信息表
     */
    @GetMapping(value = "/enterExport")
    public void enterExport(HttpServletResponse response, HttpServletRequest request,
                         @RequestParam(name = "pageNo" ,required = false) Integer pageNo,
                         @RequestParam(required = false) String enterpriseName,
                         @RequestParam String enterpriseCredittype,
                         @RequestParam String enterpriseScoreNumber,
                         @RequestParam String enterpriseIdNumber,
                         @RequestParam String enterpriseScore,
                         @RequestParam String organName,
                         @RequestParam String enterpriseClassification){

        PageRequest pageRequest = PageRequest.of(pageNo-1, 2000);
        //获取数据
        QueryResults<ApEnterpriseCreditEntity> priseCredit = enterpriseService.getAllConEnterPrise(pageRequest,enterpriseCredittype,enterpriseName,
                enterpriseScoreNumber,enterpriseIdNumber,enterpriseScore,enterpriseClassification,organName, request);
        List<ApEnterpriseCreditEntity> priseList = priseCredit.getResults();
        if(priseList.size()>=0 || priseList!=null){
            //创建导出实体
            ExcelData data = new ExcelData();
            data.setName("评家单位信用信息表");
            List<String> titles = new ArrayList();

            titles.add("评价单位评分编号");
            titles.add("评价单位名称");
            titles.add("组织机构名称");
            titles.add("评价单位证件号码");
            titles.add("评价单位分数");
            titles.add("评价单位分类");
            data.setTitles(titles);

            List<List<Object>> rows = new ArrayList();
            for (int i = 0; i < priseList.size(); i++) {
                List<Object> row = new ArrayList();
                row.add(priseList.get(i).getEnterpriseScoreNumber());
                row.add(priseList.get(i).getEnterpriseName());
                row.add(priseList.get(i).getOrganName());
                row.add(priseList.get(i).getEnterpriseIdNumber());
                row.add(priseList.get(i).getEnterpriseScore());
                if ("1".equals(priseList.get(i).getEnterpriseClassification())){
                    row.add("A");
                }else if("2".equals(priseList.get(i).getEnterpriseClassification())){
                    row.add("B");
                }else if("3".equals(priseList.get(i).getEnterpriseClassification())){
                    row.add("C");
                }else if("4".equals(priseList.get(i).getEnterpriseClassification())){
                    row.add("D");
                }else if("5".equals(priseList.get(i).getEnterpriseClassification())){
                    row.add("E");
                }
                rows.add(row);
            }
            data.setRows(rows);
            String s="";
            try {
                ExportExcelUtils.exportExcel(response,"评价单位信用信息表.xlsx",data);
                s = JSON.toJSONString(rows);
                sysAuditService.saveSuccessAudit("单位信用信息表", Constant.ENTERPRISE,Constant.PT,Constant.EXPORT,s,request);
            }catch (Exception e){
                e.printStackTrace();
                sysAuditService.saveFailAudit("单位信用信息表",Constant.ENTERPRISE,Constant.PT,Constant.EXPORT,s,e,request);
            }
        }

    }


        /**
         * 导出建设单位信用信息黑名单表
         */
        @GetMapping(value = "/enterReExport")
        public void enterReExport(HttpServletResponse response,
                             @RequestParam(name = "pageNo" ,required = false) Integer pageNo,
                             @RequestParam(required = false) String enterpriseName,
                             @RequestParam String enterpriseCredittype,
                             @RequestParam String enterpriseScoreNumber,
                             @RequestParam String enterpriseIdNumber,
                             @RequestParam String enterpriseScore,
                             @RequestParam String enterpriseClassification,HttpServletRequest request){

            PageRequest pageRequest = PageRequest.of(pageNo-1, 2000);
            //获取数据
            QueryResults<ApEnterpriseCreditEntity> priseCredit = enterpriseJSService.getAllReConEnterPrise(pageRequest,enterpriseCredittype,enterpriseName,
                    enterpriseScoreNumber,enterpriseIdNumber,enterpriseScore,enterpriseClassification,request);
            List<ApEnterpriseCreditEntity> priseList = priseCredit.getResults();

            if(priseList.size()>0 || priseList!=null){
                //创建导出实体
                ExcelData data = new ExcelData();
                data.setName("建设单位黑名单");
                List<String> titles = new ArrayList();

                titles.add("建设单位评分编号");
                titles.add("建设单位名称");
                titles.add("建设单位证件号码");
                titles.add("建设单位分数");
                titles.add("建设单位分类");
                data.setTitles(titles);

                List<List<Object>> rows = new ArrayList();
                for (int i = 0; i < priseList.size(); i++) {
                    List<Object> row = new ArrayList();
                    row.add(priseList.get(i).getEnterpriseScoreNumber());
                    row.add(priseList.get(i).getEnterpriseName());
                    row.add(priseList.get(i).getEnterpriseIdNumber());
                    row.add(priseList.get(i).getEnterpriseScore());
                    if("1".equals(priseList.get(i).getEnterpriseClassification())) {
                        row.add("A");
                    } else if("2".equals(priseList.get(i).getEnterpriseClassification())) {
                        row.add("B");
                    } else if("3".equals(priseList.get(i).getEnterpriseClassification())) {
                        row.add("C");
                    } else if("4".equals(priseList.get(i).getEnterpriseClassification())) {
                        row.add("D");
                    } else if("5".equals(priseList.get(i).getEnterpriseClassification())) {
                        row.add("E");
                    } else {
                        row.add(priseList.get(i).getEnterpriseClassification());
                    }
                    rows.add(row);
                }
                data.setRows(rows);
                String s="";
                try {
                    ExportExcelUtils.exportExcel(response,"建设单位黑名单.xlsx",data);
                    s = JSON.toJSONString(rows);
                    sysAuditService.saveSuccessAudit("单位信用信息表", Constant.ENTERJSPRISE,Constant.PT,Constant.EXPORT,s,request);
                }catch (Exception e){
                    e.printStackTrace();
                    sysAuditService.saveFailAudit("单位信用信息表",Constant.ENTERJSPRISE,Constant.PT,Constant.EXPORT,s,e,request);
                }
            }

        }

    /**
     * 导出评价单位黑名单表
     */
    @GetMapping(value = "/enterPjReExport")
    public void enterPjReExport(HttpServletResponse response,
                           @RequestParam(name = "pageNo" ,required = false) Integer pageNo,
                           @RequestParam(required = false) String enterpriseName,
                           @RequestParam String enterpriseCredittype,
                           @RequestParam String enterpriseScoreNumber,
                           @RequestParam String enterpriseIdNumber,
                           @RequestParam String enterpriseScore,
                           @RequestParam String enterpriseClassification,
                             HttpServletRequest request){

        PageRequest pageRequest = PageRequest.of(pageNo-1, 2000);
        //获取数据
        QueryResults<ApEnterpriseCreditEntity> priseCredit = enterpriseService.getAllReConEnterPrise(pageRequest,enterpriseCredittype,enterpriseName,
                enterpriseScoreNumber,enterpriseIdNumber,enterpriseScore,enterpriseClassification,request);
        List<ApEnterpriseCreditEntity> priseList = priseCredit.getResults();

        if(priseList.size()>0 || priseList!=null){
            //创建导出实体
            ExcelData data = new ExcelData();
            data.setName("评价单位黑名单");
            List<String> titles = new ArrayList();

            titles.add("评价单位评分编号");
            titles.add("评价单位名称");
            titles.add("评价单位证件号码");
            titles.add("评价单位分数");
            titles.add("评价单位分类");
            data.setTitles(titles);

            List<List<Object>> rows = new ArrayList();
            for (int i = 0; i < priseList.size(); i++) {
                List<Object> row = new ArrayList();
                row.add(priseList.get(i).getEnterpriseScoreNumber());
                row.add(priseList.get(i).getEnterpriseName());
                row.add(priseList.get(i).getEnterpriseIdNumber());
                row.add(priseList.get(i).getEnterpriseScore());
                if("1".equals(priseList.get(i).getEnterpriseClassification())) {
                    row.add("A");
                } else if("2".equals(priseList.get(i).getEnterpriseClassification())) {
                    row.add("B");
                } else if("3".equals(priseList.get(i).getEnterpriseClassification())) {
                    row.add("C");
                } else if("4".equals(priseList.get(i).getEnterpriseClassification())) {
                    row.add("D");
                } else if("5".equals(priseList.get(i).getEnterpriseClassification())) {
                    row.add("E");
                } else {
                    row.add(priseList.get(i).getEnterpriseClassification());
                }
                rows.add(row);
            }
            data.setRows(rows);
            String s="";
            try {
                ExportExcelUtils.exportExcel(response,"评价单位黑名单.xlsx",data);
                s = JSON.toJSONString(rows);
                sysAuditService.saveSuccessAudit("单位信用信息表", Constant.ENTERPRISE,Constant.PT,Constant.EXPORT,s,request);
            }catch (Exception e){
                e.printStackTrace();
                sysAuditService.saveFailAudit("单位信用信息表",Constant.ENTERPRISE,Constant.PT,Constant.EXPORT,s,e,request);
            }
        }

    }

    /**
     * 导出专家黑名单
     */
     @GetMapping(value = "/expertReExport")
     public void expertReExport(HttpServletResponse response,
                                    @RequestParam(required = false) String expertName,
                                    @RequestParam(required = false) String expertIdcard,
                                    @RequestParam(required = false) String certype,
                                    @RequestParam(required = false) String expertCode,
                                    @RequestParam(required = false) String expertScore,
                                    @RequestParam(required = false) String expertLevl,
                                    @RequestParam(required = false) String expertClassification,
                                    @RequestParam(required = false) Integer pageNo){

         PageRequest pageRequest = PageRequest.of(pageNo-1, 2000);
         QueryResults<ApExpertCreditEntity> expertCred = expertCreditService.getAllReConExpertCred(pageRequest,expertName,expertIdcard,certype,expertCode,expertScore,expertLevl,expertClassification);

         List<ApExpertCreditEntity> expertList = expertCred.getResults();

         if(expertList.size()>0 || expertList!=null){
             //创建导出实体
             ExcelData data = new ExcelData();
             data.setName("专家黑名单");
             List<String> titles = new ArrayList();

             titles.add("专家评分编号");
             titles.add("专家名称");
             titles.add("证件号码");
             titles.add("证件类型");
             titles.add("专家信用分数");
             titles.add("专家分类");
             titles.add("专家等级");
             data.setTitles(titles);

             List<List<Object>> rows = new ArrayList();
             for (int i = 0; i < expertList.size(); i++) {
                 List<Object> row = new ArrayList();
                 row.add(expertList.get(i).getExpertCode());
                 row.add(expertList.get(i).getExpertName());
                 row.add(expertList.get(i).getExpertIdcard());
                 if("111".equals(expertList.get(i).getCertype())) {
                     row.add("居民身份证");
                 } else if("112".equals(expertList.get(i).getCertype())) {
                    row.add("临时居民身份证");
                 } else if("113".equals(expertList.get(i).getCertype())) {
                     row.add("户口簿");
                 } else if("114".equals(expertList.get(i).getCertype())) {
                     row.add("中国人民解放军军官证");
                 } else if("115".equals(expertList.get(i).getCertype())) {
                     row.add("中国人民武警警察部队警官证");
                 } else if("117".equals(expertList.get(i).getCertype())) {
                     row.add("出生医学证明");
                 } else if("118".equals(expertList.get(i).getCertype())) {
                     row.add("中国人民解放军士兵证");
                 } else if("119".equals(expertList.get(i).getCertype())) {
                     row.add("中国人民武警警察部队士兵证");
                 } else if("120".equals(expertList.get(i).getCertype())) {
                     row.add("中国人民解放军文职人员证");
                 } else if("122".equals(expertList.get(i).getCertype())) {
                     row.add("中国人民武装警察部队文职人员证");
                 } else if("154".equals(expertList.get(i).getCertype())) {
                     row.add("居住证");
                 } else if("411".equals(expertList.get(i).getCertype())) {
                     row.add("外交护照");
                 } else if("412".equals(expertList.get(i).getCertype())) {
                     row.add("公务护照");
                 } else if("413".equals(expertList.get(i).getCertype())) {
                     row.add("公务普通护照");
                 } else if("414".equals(expertList.get(i).getCertype())) {
                     row.add("普通护照");
                 } else if("415".equals(expertList.get(i).getCertype())) {
                     row.add("旅行证");
                 } else {
                     row.add(expertList.get(i).getCertype());
                 }
                 row.add(expertList.get(i).getExpertScore());
                 if("1".equals(expertList.get(i).getExpertClassification())) {
                    row.add("A");
                 } else if("2".equals(expertList.get(i).getExpertClassification())) {
                     row.add("B");
                 } else if("3".equals(expertList.get(i).getExpertClassification())) {
                     row.add("C");
                 } else if("4".equals(expertList.get(i).getExpertClassification())) {
                     row.add("D");
                 } else if("5".equals(expertList.get(i).getExpertClassification())) {
                     row.add("E");
                 } else {
                     row.add(expertList.get(i).getExpertClassification());
                 }
                 if("1".equals(expertList.get(i).getExpertLevl())) {
                     row.add("省级");
                 } else if("2".equals(expertList.get(i).getExpertLevl())) {
                     row.add("国家级");
                 } else {
                     row.add(expertList.get(i).getExpertLevl());
                 }
                 rows.add(row);
             }
             data.setRows(rows);
             String s="";
             try {
                 ExportExcelUtils.exportExcel(response,"专家黑名单.xlsx",data);
                 s = JSON.toJSONString(rows);
                 sysAuditService.saveSuccessAudit("专家信用信息", Constant.EXPERTCRED,Constant.PT,Constant.EXPORT,s,request);

             }catch (Exception e){
                 e.printStackTrace();
                 sysAuditService.saveFailAudit("专家信用信息",Constant.EXPERTCRED,Constant.PT,Constant.EXPORT,s,e,request);
             }
         }

     }

}
